package main;

import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

import com.myLogger.LoggerManager;

public class MyExceptionHandler extends ExceptionHandlerWrapper {

	@ManagedProperty(value="#{log.logger}")
	private LoggerManager log;
	private ExceptionHandler wrapped;

	public MyExceptionHandler(ExceptionHandler wrapped) {
		this.wrapped = wrapped;
	}

	@Override
	public ExceptionHandler getWrapped() {
		return wrapped;
	}

	@Override
	public void handle() throws FacesException {
		//Iterate over all unhandeled exceptions
		Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
		while (i.hasNext()) {
			ExceptionQueuedEvent event = (ExceptionQueuedEvent) i.next();
			ExceptionQueuedEventContext context =
					(ExceptionQueuedEventContext)event.getSource();

			//obtain throwable object
			Throwable t = context.getException();

			//here you do what ever you want with exception
			try{
				//log error
				log.logError("Serious error happened!", (Exception) t);
				//redirect to error view etc....  
			}finally{
				//after exception is handeled, remove it from queue
				i.remove();
			}
		}
		//let the parent handle the rest
		getWrapped().handle();
	}

	public void setLog(LoggerManager log) {
		this.log = log;
	}	
}